package com.tyf.common.web.controller;

import com.tyf.common.redis.RedisManager;
import com.tyf.common.exception.MyselfMsgException;
import com.tyf.common.util.CommonUtils;
import com.tyf.common.vo.SystemUser;
import com.tyf.common.web.bind.annotation.CurrentUser;
import com.tyf.common.dao.hibernate.HibernateUtils;
import com.tyf.common.util.BizUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * 
 * Description: 应用程序管理控制器
 * author:      liutao
 * Createdate:  2016年8月2日下午6:56:14
 */

@Controller
@RequestMapping(value = "/manager")
public class AppManagerController extends BaseController{
	@PersistenceContext
    EntityManager em;
	@Autowired(required = false)
    RedisManager redisManager;
	final static Logger logger = LoggerFactory.getLogger(AppManagerController.class);
	
	
	@RequestMapping("/em/evictL1C.json")
	@ResponseBody
	public Map<String, Object> evictL1C(@CurrentUser SystemUser currentUser) {
		return managerOperation(currentUser, n -> HibernateUtils.evictLevel1Cache(em));
	}
	
	@RequestMapping("/em/evictL2C.json")
	@ResponseBody
	public Map<String, Object> evictL2C(@CurrentUser SystemUser currentUser) {
		return managerOperation(currentUser, n -> HibernateUtils.evictLevel2Cache(em));
	}

	@RequestMapping("/cache/remove.json")
	@ResponseBody
	public Map<String, Object> removeCache(HttpServletRequest req, @RequestParam("key") String key,
										   @CurrentUser SystemUser currentUser) {
		return managerOperation(currentUser, n -> redisManager.remove(key));
	}

	@RequestMapping("/cache/get.json")
	@ResponseBody
	public Map<String, Object> getCacheValue(HttpServletRequest req, @RequestParam("key") String key,
										   @CurrentUser SystemUser currentUser) {
		return managerOperationAndReturn(currentUser, n -> redisManager.get(key));
	}

	@RequestMapping("/cache/getEntries.json")
	@ResponseBody
	public Map<String, Object> getCacheEntries(HttpServletRequest req, @RequestParam("key") String key,
										@CurrentUser SystemUser currentUser) {
		return managerOperationAndReturn(currentUser, n -> redisManager.getEntries(key));
	}

	@RequestMapping("/cache/removeBaseCache.json")
	@ResponseBody
	public Map<String, Object> removeBaseCache(HttpServletRequest req, @CurrentUser SystemUser currentUser) {
		return managerOperation(currentUser, n -> redisManager.removePattern("Bas*"));
	}

	/**
	 * 管理操作公共方法
	 * @param currentUser
	 * @param action
	 */
	public Map<String, Object> managerOperation(SystemUser currentUser, Consumer<String> action){
		Map<String, Object> resultMap  = CommonUtils.getDefaultResultMap();
		if(BizUtils.isSuperAdmin(currentUser)){
			action.accept(null);
		}else{
			throw new MyselfMsgException("非超级管理员无权限进行此操作");
		}
		return resultMap;
	}

	/**
	 * 管理操作公共方法
	 * @param currentUser
	 * @param action
	 */
	private Map<String, Object> managerOperationAndReturn(SystemUser currentUser,  Function<String, Object> action){
		Map<String, Object> resultMap = CommonUtils.getDefaultResultMap();
		Object data = null;
		if(BizUtils.isSuperAdmin(currentUser)){
			data = action.apply(null);
		}else{
			throw new MyselfMsgException("非超级管理员无权限进行此操作");
		}

		if(data != null){
			resultMap.put("data", data);
		}
		return resultMap;
	}

	@Deprecated
	public Map<String, Object> managerOperation(SystemUser currentUser, Consumer<String> action, Map<String, Object> resultMap){
		if (resultMap == null) {
			resultMap = CommonUtils.getDefaultResultMap();
		}
		if(BizUtils.isSuperAdmin(currentUser)){
			action.accept(null);
		}else{
			throw new MyselfMsgException("非超级管理员无权限进行此操作");
		}
		return resultMap;
	}

	@RequestMapping("/logger")
	@ResponseBody
	public Map<String, Object> logger(){
		String level = "default";
		if(logger.isDebugEnabled()){
			level = "debug";
			logger.debug("=====> " + level);
		}else if (logger.isInfoEnabled()){
			level = "info";
			logger.info("=====> " + level);
		}else if (logger.isErrorEnabled()){
			level = "error";
			logger.error("=====> " + level);
		}else if(logger.isTraceEnabled()){
			level = "trace";
			logger.trace("=====> " + level);
		}else if(logger.isWarnEnabled()){
			level = "warn";
			logger.warn("=====> " + level);
		}

		return CommonUtils.getDefaultResultMap("cuurent root loglevel [" + level +"]");
	}
}
